#if !NETCF
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace TelnetExpect
{
    /// <summary>
    /// An implementation of <see cref="T:AnsiConsoleBase2"/> that uses
    /// <see cref="T:System.Console">System.Console</see>.
    /// </summary>
    public class ConsoleAnsiConsole : AnsiConsoleBase2
    {
        public ConsoleAnsiConsole(Stream peer)
            : base(peer)
        {
            string txt = System.Configuration.ConfigurationManager.AppSettings["AnsiConsoleLogging"];
            bool enabled = Convert.ToBoolean(txt, System.Globalization.CultureInfo.InvariantCulture);
            m_logText = enabled;
        }

        protected override void OnText(byte[] text, int offset, int count)
        {
            Encoding enc = Encoding.ASCII;
            enc = Encoding.GetEncoding(437);
            string str = enc.GetString(text, offset, count);
            if (m_logText) {
                string str2 = ShowControlChars(str);
                Trace.WriteLine("Text: >>" + str2 + "<<");
            }
            Console.Write(str);
        }

        //----
        protected override void SetCursorPosition(int row, int column)
        {
            Console.SetCursorPosition(column, row);
        }

        //----
        protected override void EraseInLine(ClearAction action)
        {
            if (action == ClearAction.ToEnd) {
                int left = Console.CursorLeft;
                int top = Console.CursorTop;
                try {
                    int fudge = 1;
                    for (int i = left; i < Console.WindowWidth - fudge; ++i) {
                        Console.Write(" ");
                    }
                } finally {
                    Console.CursorLeft = left;
                    Console.CursorTop = top;
                }
            } else {
                Trace.WriteLine(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                    "Skipping K (clear line) action '{0}'", action));
            }
        }

        //----
        protected override void SetForegroundColor(ConsoleColor color)
        {
            Console.ForegroundColor = color;
        }

        protected override void SetBackgroundColor(ConsoleColor color)
        {
            Console.BackgroundColor = color;
        }

        protected override void UpdateColors(string debugCommandName)
        {
            ConsoleColor color, cc2;
            color = Console.ForegroundColor;
            cc2 = GetColorWithIntensity(color, CurrentColorIntensity);
            Console.ForegroundColor = cc2;
            //
            color = Console.BackgroundColor;
            cc2 = GetColorWithIntensity(color, Intensity.Bold);
            if (cc2 == ConsoleColor.DarkGray) {
                cc2 = ConsoleColor.Black;
            }
            Console.BackgroundColor = cc2;
            //
            Trace.WriteLine(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                "On {0,-13}: i/fg/bg: {1,-6} {2,-7} {3,-7} --> fg: {4,-11}, bg: {5,-11}",
                debugCommandName,
                CurrentColorIntensity, CurrentColorFg, CurrentColorBg,
                Console.ForegroundColor, Console.BackgroundColor));
        }

    }
}
#endif
